<template>
  <div class="inbound-container">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>入库管理</h1>
    </div>
    
    <div class="page-content">
      <!-- 入库操作区 -->
      <div class="operation-card">
        <h2>新增入库记录</h2>
        <form class="inbound-form">
          <div class="form-row">
            <div class="form-group">
              <label>耗材名称</label>
              <select v-model="selectedConsumable">
                <option value="">请选择耗材</option>
                <option v-for="item in consumables" :key="item.id" :value="item">{{ item.name }}</option>
              </select>
            </div>
            <div class="form-group">
              <label>入库数量</label>
              <input type="number" v-model="inboundQuantity" min="1" placeholder="请输入数量">
            </div>
          </div>
          
          <div class="form-row">
            <div class="form-group">
              <label>供应商</label>
              <input type="text" v-model="supplier" placeholder="请输入供应商">
            </div>
            <div class="form-group">
              <label>入库日期</label>
              <input type="date" v-model="inboundDate">
            </div>
          </div>
          
          <div class="form-group full-width">
            <label>备注信息</label>
            <textarea v-model="remarks" rows="3" placeholder="请输入备注信息"></textarea>
          </div>
          
          <button type="button" class="submit-btn" @click="addInboundRecord">提交入库</button>
        </form>
      </div>
      
      <!-- 入库记录列表 -->
      <div class="records-card">
        <div class="card-header">
          <h2>入库记录列表</h2>
          <div class="search-box">
            <input type="text" v-model="searchKeyword" placeholder="搜索记录...">
            <button class="search-btn">搜索</button>
          </div>
        </div>
        
        <table class="records-table">
          <thead>
            <tr>
              <th>ID</th>
              <th>耗材名称</th>
              <th>规格型号</th>
              <th>入库数量</th>
              <th>供应商</th>
              <th>入库日期</th>
              <th>操作人</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="record in filteredRecords" :key="record.id">
              <td>{{ record.id }}</td>
              <td>{{ record.consumableName }}</td>
              <td>{{ record.specification }}</td>
              <td>{{ record.quantity }}</td>
              <td>{{ record.supplier }}</td>
              <td>{{ formatDate(record.date) }}</td>
              <td>{{ record.operator }}</td>
              <td>
                <button class="view-btn" @click="viewRecord(record)">查看</button>
              </td>
            </tr>
            <tr v-if="filteredRecords.length === 0">
              <td colspan="8" class="no-data">暂无入库记录</td>
            </tr>
          </tbody>
        </table>
        
        <div class="pagination">
          <button :disabled="currentPage === 1" @click="currentPage--">上一页</button>
          <span>{{ currentPage }} / {{ totalPages }}</span>
          <button :disabled="currentPage === totalPages" @click="currentPage++">下一页</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'InboundManage',
  data() {
    return {
      // 表单数据
      selectedConsumable: '',
      inboundQuantity: 1,
      supplier: '',
      inboundDate: new Date().toISOString().split('T')[0],
      remarks: '',
      
      // 耗材列表
      consumables: [
        { id: 1, name: '打印机墨盒', specification: 'HP-12A' },
        { id: 2, name: '复印纸', specification: 'A4-70g' },
        { id: 3, name: 'U盘', specification: '32GB' },
        { id: 4, name: '鼠标', specification: '无线' },
        { id: 5, name: '键盘', specification: '机械' }
      ],
      
      // 入库记录
      inboundRecords: [
        { 
          id: 1, 
          consumableName: '打印机墨盒', 
          specification: 'HP-12A',
          quantity: 10, 
          supplier: '惠普科技', 
          date: '2023-05-15', 
          operator: '张三',
          remarks: '月度常规采购'
        },
        { 
          id: 2, 
          consumableName: '复印纸', 
          specification: 'A4-70g',
          quantity: 50, 
          supplier: '得力办公', 
          date: '2023-05-10', 
          operator: '李四',
          remarks: '季度采购'
        }
      ],
      
      // 搜索和分页
      searchKeyword: '',
      currentPage: 1,
      pageSize: 10
    };
  },
  computed: {
    filteredRecords() {
      return this.inboundRecords.filter(record => 
        record.consumableName.includes(this.searchKeyword) ||
        record.supplier.includes(this.searchKeyword) ||
        record.operator.includes(this.searchKeyword)
      );
    },
    totalPages() {
      return Math.ceil(this.filteredRecords.length / this.pageSize);
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    addInboundRecord() {
      if (!this.selectedConsumable || !this.inboundQuantity || !this.supplier || !this.inboundDate) {
        alert('请填写完整信息');
        return;
      }
      
      const newRecord = {
        id: this.inboundRecords.length + 1,
        consumableName: this.selectedConsumable.name,
        specification: this.selectedConsumable.specification,
        quantity: this.inboundQuantity,
        supplier: this.supplier,
        date: this.inboundDate,
        operator: '当前用户',
        remarks: this.remarks
      };
      
      this.inboundRecords.unshift(newRecord);
      
      // 重置表单
      this.selectedConsumable = '';
      this.inboundQuantity = 1;
      this.supplier = '';
      this.inboundDate = new Date().toISOString().split('T')[0];
      this.remarks = '';
      
      alert('入库记录添加成功');
    },
    viewRecord(record) {
      alert(`
        入库详情:
        ID: ${record.id}
        耗材名称: ${record.consumableName}
        规格型号: ${record.specification}
        入库数量: ${record.quantity}
        供应商: ${record.supplier}
        入库日期: ${record.date}
        操作人: ${record.operator}
        备注: ${record.remarks || '无'}
      `);
    },
    formatDate(dateString) {
      const date = new Date(dateString);
      return date.toLocaleDateString('zh-CN');
    }
  }
};
</script>

<style scoped>
.inbound-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1e5e8;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.icon-arrow-left::before {
  content: "←";
}

.page-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.operation-card, .records-card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
  transition: box-shadow 0.3s;
}

.operation-card:hover, .records-card:hover {
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

h2 {
  color: #333;
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 18px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.inbound-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-row {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}

.form-group {
  flex: 1;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group label {
  font-size: 14px;
  color: #666;
}

.form-group input, 
.form-group select, 
.form-group textarea {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus, 
.form-group select:focus, 
.form-group textarea:focus {
  border-color: #3498db;
  outline: none;
}

.full-width {
  width: 100%;
}

.submit-btn {
  align-self: flex-start;
  padding: 10px 20px;
  background: #2ecc71;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
}

.submit-btn:hover {
  background: #27ae60;
  transform: translateY(-2px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-box {
  display: flex;
  gap: 10px;
}

.search-box input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  width: 200px;
}

.search-btn {
  padding: 8px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
}

.search-btn:hover {
  background: #2980b9;
}

.records-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.records-table th, 
.records-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.records-table th {
  background-color: #f9fafb;
  font-weight: 600;
  color: #333;
}

.records-table tr:hover {
  background-color: #f9fafb;
}

.view-btn {
  padding: 5px 10px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.3s;
  font-size: 13px;
}

.view-btn:hover {
  background: #2980b9;
}

.no-data {
  text-align: center;
  color: #999;
  padding: 30px;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  margin-top: 10px;
}

.pagination button {
  padding: 5px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #f5f5f5;
  border-color: #ccc;
}

.pagination button:disabled {
  color: #999;
  cursor: not-allowed;
}

.pagination span {
  color: #666;
}
</style>